package com.czxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.domain.Course;
import com.czxy.domain.Student;
import com.czxy.feignclient.TeacherClient;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.CourseMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.service.StudentService;
import com.czxy.vo.StudentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClassesMapper classesMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TeacherClient teacherClient;

    /**
     * 多条件分页搜索
     * @param page
     * @param pagesize
     * @param studentVo
     * @return
     */
    @Override
    public Page<Student> findByCondition(Integer page, Integer pagesize, StudentVo studentVo) {
        //1 多条件
        String stuname = studentVo.getStuname();
        Integer minAge = studentVo.getMinAge();
        Integer maxAge = studentVo.getMaxAge();
        String sex = studentVo.getSex();
        Integer classid = studentVo.getClassid();

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (stuname!=null&& StringUtils.isNotBlank(stuname)){
            queryWrapper.like("stuname",stuname);
        }
        if (minAge!=null&&StringUtils.isNotBlank(minAge.toString())){
            queryWrapper.ge("age",minAge);
        }
        if (maxAge!=null&&StringUtils.isNotBlank(maxAge.toString())){
            queryWrapper.le("age",maxAge);
        }
        if (sex!=null&&StringUtils.isNotBlank(sex)){
            queryWrapper.eq("sex",sex);
        }
        if (classid!=null&&StringUtils.isNotBlank(classid.toString())){
            queryWrapper.eq("classid",classid);
        }
        //2 分页
        Page<Student> studentPage = new Page<>(page, pagesize);
        //执行查询
        baseMapper.selectPage(studentPage,queryWrapper);

        //遍历recoreds，查询班级信息
        for (Student stu:studentPage.getRecords()){
            stu.setClasses(classesMapper.selectById(stu.getClassid()));
            //查找所选科目
            stu.setCourseList(courseMapper.findCourseByStuid(stu.getStuid()));

            ArrayList<Integer> courseIdList = new ArrayList<>();
            for (Course course:stu.getCourseList()){
                courseIdList.add(course.getCourseid());
            }
            stu.setCourseIdList(courseIdList);




            //发起远程调用
            stu.setTeacher(teacherClient.findTeacherByTeaid(stu.getCreateId()));
            //查找学生的时候，需要将学生所在的省市区字符串转成省市区数组
            stu.setCityIdList(Arrays.asList(stu.getCityIds().split(",")));
        }

        return studentPage;
    }
}
